<?php

/**
* ShowSlideFormModule.class.php
*
* Concrete page module responsable for creating slide show list
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/

require_once("framework/config.php");
require_once("framework/PageModule.class.php");
require_once("MDB2.php");
require_once("HTML/QuickForm.php");
require_once("HTML/QuickForm/Renderer/Default.php");
require_once("framework/Wurfl.class.php");
require_once("framework/Yt.class.php");
require_once("Directions.class.php");
require_once("framework/LocalizationSingleton.class.php");

// {{{ SlideShowListModule

class ShowSlideFormModule extends PageModule 
{
    // {{{ Class operations 

    // {{{ SlideShowListModule
    /**
    * C'tor
    */
    
    function ShowSlideFormModule()
    {
        trigger_error("ENTER ShowSlideFormModule::ShowSlideFormModule()", E_USER_NOTICE);
    }
    // }}}
    // {{{ getContent

    /**
    * Function returns page content
    *
    */

    function getContent()
    {

        trigger_error("ENTER ShowSlideFormModule::getContent()", E_USER_NOTICE);
        $options = &PEAR::getStaticProperty('CONFIG_INI','TEMPLATES'); 
 
        $tpl = new HTML_Template_IT($options["templates_dir"]);
        $tpl->loadTemplatefile("slide.tpl.xhtml",true,true);
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $dbh = new PDO($options['dsn_string']);
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sth = $dbh->prepare("SELECT * FROM slide WHERE rowid = :id");
        $sth->bindParam(':id',$_GET['id']);
        $sth->execute();
        $guideRow = $sth->fetch(PDO::FETCH_ASSOC);

        $uploadOpt = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $globalOpt = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');

        $tpl->setVariable("NAME",$guideRow['name']);

        if ( $guideRow['pic_file'] ) 
        {
            $tpl->setCurrentBlock('image_row');
            $tpl->setVariable("URL",$globalOpt['service_url']."send_image.php?id=".$guideRow['pic_file']);
            $tpl->setVariable("ALT",$guideRow['pic_file']);
            $tpl->parseCurrentBlock('image_row');
        }

        if ( $guideRow['desc'] ) 
        {
            $tpl->setCurrentBlock('desc_row');
            $tpl->setVariable("DESC",$guideRow['desc']);
            $tpl->parseCurrentBlock('desc_row');
        }

        if ( $guideRow['sound_file'] ) 
        {
            $tpl->setCurrentBlock('sound_row');
            $tpl->setVariable("URL",$globalOpt['service_url']."send_sound.php?id=".$guideRow['sound_file']);
            $tpl->setVariable("TITLE",_('Pobierz opis dźwiękowy'));
            
            $tpl->parseCurrentBlock('sound_row');
        }
        
        if ( Wurfl::deviceFactory()->getCapability("streaming_video") )
        {
            if ( $guideRow['video_file'] ) 
            {

                $yt = Yt::getClient();
                try
                {
                    $videoEntry = $yt->getVideoEntry($guideRow['video_file']);
                    if (Wurfl::deviceFactory()->getCapability("model_name") == 'iPhone' || 
                        Wurfl::deviceFactory()->getCapability("is_wireless_device") != 'true' )
                    {
                        
                        foreach ($videoEntry->mediaGroup->content as $content) {
                            if ( $content->format === '5') {
                                    $tpl->setCurrentBlock('video_embed_row');
                                    $tpl->setVariable("URL",$content->url);
                                    $tpl->setVariable("CONTENTTYPE",$content->type);
                                    
                                    $tpl->setVariable("HEIGHT", Wurfl::getHeight() );
                                    $tpl->setVariable("WIDTH", Wurfl::getWidth() );

                                    $tpl->parseCurrentBlock('video_embed_row');
                            }
                        }
                    }
                    elseif (Wurfl::deviceFactory()->getCapability("streaming_vcodec_mpeg4_sp") >=0)
                    {
                        foreach ($videoEntry->mediaGroup->content as $content) {
                            if ( $content->format === '6') {
                                $tpl->setCurrentBlock('video_link_row');
                                $tpl->setVariable("URL",$content->url);
                                $tpl->setVariable("TITLE",_('Pobierz video'));
                                
                                $tpl->parseCurrentBlock('video_link_row');
                            }
                        }
                    }
                    elseif( Wurfl::deviceFactory()->getCapability("streaming_vcodec_h263_0") >= 0 || 
                            Wurfl::deviceFactory()->getCapability("streaming_vcodec_h263_3") >= 0 )
                    {
                        foreach ($videoEntry->mediaGroup->content as $content) {
                            if ( $content->format === '1') {
                                $tpl->setCurrentBlock('video_link_row');
                                $tpl->setVariable("URL",$content->url);
                                $tpl->setVariable("TITLE",_('Pobierz video'));
                                $tpl->parseCurrentBlock('video_link_row');
                            }
                        }

                    }
                    else
                    {
                        $tpl->setCurrentBlock('video_status');
                        $tpl->setVariable("TEXT",_('Wideo niedostępne na twoim telefonie.'));
                        $tpl->parseCurrentBlock('video_status');
                    }


                }
                catch ( Zend_Exception $e )
                {
                    $tpl->setCurrentBlock('video_status');
                    $tpl->setVariable("TEXT",_('Wideo w trakcie przetwarzania'));
                    $tpl->parseCurrentBlock('video_status');
                }
        


            }
        }
        if ( $guideRow['geocode'] ) 
        {
            
            $tpl->setCurrentBlock('location_row');
            $tpl->setVariable("LOCATION",$guideRow['geocode']);
            $tpl->setVariable("MAP",_('Jesteś tutaj').'  "A"');
            $tpl->parseCurrentBlock('location_row');

            $sth = $dbh->prepare("SELECT * FROM slide WHERE guide_id = :id AND `order` > :order ORDER BY `order` ASC");
            $sth->bindParam(':id',$guideRow['guide_id']);
            $sth->bindParam(':order',$guideRow['order']);
            $sth->execute();
            $dstRow = $sth->fetch(PDO::FETCH_ASSOC);

            if( $dstRow['geocode'] )
            {
                $tpl->setCurrentBlock('map_row_with_path');

                list($lang,$locale) = split("_",LocalizationSingleton::instance()->getLocale());
                $direction = new Directions($guideRow['geocode'],$dstRow['geocode'],$lang);

                $tpl->setVariable("ENCPOLYLINE", $direction->getEncPolyline() );
                $tpl->setVariable("ALT",$guideRow['geocode']."->".$dstRow['geocode']);
                $tpl->setVariable("SENSOR","false");

                $tpl->setVariable("HEIGHT", Wurfl::getHeight() );
                $tpl->setVariable("WIDTH", Wurfl::getWidth() );


                $tpl->setVariable("LABEL1","A");
                $tpl->setVariable("MARKERLOCATION1",$guideRow['geocode']);
    
                $tpl->setVariable("LABEL2","B");
                $tpl->setVariable("MARKERLOCATION2",$dstRow['geocode']);

                $tpl->parseCurrentBlock('map_row_with_path');

                $tpl->setCurrentBlock('next_location_row');
                $tpl->setVariable("LOCATION",$dstRow['name'].",".$dstRow['geocode']);
                $tpl->setVariable("NEXTLOCATION",_('Następna lokalizacja').'  "B"');
                $tpl->parseCurrentBlock('next_location_row');

                $instructions = $direction->getHtmlInstructions();
                $id = 1;
                foreach ( $instructions as $instruction  ) 
                {
                    $tpl->setCurrentBlock('next_location_instructions_row');
                    $tpl->setVariable("ID",$id);
                    $tpl->setVariable("INSTRUCTION",$instruction);
                    $tpl->parseCurrentBlock('next_location_instructions_row');
                    $id++;
                }                
            }
            else
            {

                $tpl->setCurrentBlock('map_row');

                $tpl->setVariable("ALT",$guideRow['geocode']);
                $tpl->setVariable("SENSOR","false");

                $tpl->setVariable("HEIGHT", Wurfl::getHeight() );
                $tpl->setVariable("WIDTH", Wurfl::getWidth() );

                $tpl->setVariable("MARKERSIZE","tiny");
                $tpl->setVariable("MARKERLOCATION",$guideRow['geocode']);
                
                $tpl->parseCurrentBlock('map_row');
            }

    




        }
        return $tpl->get();
    }
    
    // }}}

    // }}}
}

// }}} 
?>
